package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"softwareEngineering/models"
	"strconv"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {

	//c.Data["user"] = user
	u := c.Ctx.Input.Session("user")
	if u == nil {
		c.TplName = "index.html"

	} else {
		user := c.Ctx.Input.Session("user").(models.User)
		c.Data["user"] = user
	}
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	books := QueryBookData()
	c.Data["books"] = books
	c.Data["homeImg"] = books[0].HomePage
	c.TplName = "index.html"
}

func (c *MainController) Home() {
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "imageShow.html"
}

func (c *MainController) BaseKnowledge() {
	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.html#baseKnowledge"
}

//退出登录函数
func (c *MainController) ExitLogin() {
	c.DelSession("user")

	beego.Info("exit  successful")
	c.TplName = "index.html"

}

func (c *MainController) Insert() {
	//1. 有ORM对象
	o := orm.NewOrm()
	//2.有一个要插入数据的结构体对象
	user := models.User{}
	//3.对结构体赋值
	user.Name = "111"
	user.Pwd = "222"
	//4.插入
	_, err := o.Insert(&user)
	if err != nil {
		fmt.Println("Insert error", err)
		return
	} else {
		c.Ctx.WriteString("Insert successful")
	}

	c.Data["Website"] = "beego.me"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "imageShow.html"
}

func (c *MainController) QueryUser() {
	//	1. ORM 对象
	o := orm.NewOrm()
	//	2. 查询的对象
	user := models.User{}
	//	3. 指定查询对象的字段值
	//user.Id = 12
	username := c.GetString("username")
	beego.Info("username:", username)
	user.Name = username
	//	4. 查询
	err := o.Read(&user, "Name")
	if err != nil {
		beego.Info("query failure", err)
		c.Data["qf"] = err
	} else {
		beego.Info("query success", user)
		c.Data["qs"] = user
	}
	c.TplName = "imageShow.html"
}
func (c *MainController) UpdateUser() {
	//	1. 创建ORM对象
	o := orm.NewOrm()
	//	2. 需要更新的结构体对象
	user := models.User{}
	//	3. 查到需要更新的数据
	updateUser := c.GetString("updateUser")
	user.Name = updateUser
	err := o.Read(&user, "Name")
	//	4. 给数据重新赋值
	if err != nil {
		beego.Info("user is not exist")
	} else {
		//	5. 更新
		user.Name = "222"
		user.Pwd = "333"
		_, err = o.Update(&user)
		if err != nil {
			beego.Info("update failure", err)
		} else {
			beego.Info("update success")
			c.Data["us"] = "update success"
		}
	}

	c.TplName = "imageShow.html"
}

func (c *MainController) DeleteUser() {
	//	1. 创建ORM对象
	o := orm.NewOrm()
	//	2. 需要删除的结构体对象
	user := models.User{}
	//	3. 查到需要删除的数据
	deleteUserId := c.GetString("deleteUserId")

	user.Id, _ = strconv.Atoi(deleteUserId)
	beego.Info("deleteUserId:", user.Id)
	//	4. 删除
	n, err := o.Delete(&user)
	if n > 0 && err == nil {
		beego.Info("delete success")
		c.Data["ds"] = "delete success"
	} else {
		beego.Info("delete failure", err)
		c.Data["ds"] = "delete failure"
	}
	c.TplName = "imageShow.html"
}
func (c *MainController) ShowComplete() {
	c.TplName = "completeCase.html"
}
